package com.yonyou.pmclouds.messagenotice.service.rmiimpl;

import com.alibaba.dubbo.config.annotation.Service;
import com.yonyou.pmclouds.ApplicationConsts;
import com.yonyou.pmclouds.basecom.entity.PageObject;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.basecom.util.CommonProgramConsts;
import com.yonyou.pmclouds.messagenotice.entity.MessageNoticeVO;
import com.yonyou.pmclouds.messagenotice.mapper.MessageNoticeMapper;
import com.yonyou.pmclouds.messagenotice.rmiitf.MessageNoticeQuery;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

@Service(version = ApplicationConsts.APPLICATION_VERSION, interfaceClass = MessageNoticeQuery.class)
public class MessageNoticeQueryImpl implements MessageNoticeQuery {

    private final MessageNoticeMapper noticeMapper;

    @Autowired
    public MessageNoticeQueryImpl(MessageNoticeMapper noticeMapper) {
        this.noticeMapper = noticeMapper;
    }

    @Override
    public PageObject<MessageNoticeVO> getNoticesByUser(String tenantId, String userId, int pageStart, int pageSize) throws BusinessException {
        if(pageSize > CommonProgramConsts.DEFAULT_MAX_QUERY_SIZE) {
            pageSize = CommonProgramConsts.DEFAULT_MAX_QUERY_SIZE;
        }
        if(pageSize < CommonProgramConsts.DEFAULT_MIN_QUERY_SIZE) {
            pageSize = CommonProgramConsts.DEFAULT_MIN_QUERY_SIZE;
        }
        // 不需要区分租户
        int count = noticeMapper.countNoticesByUser(tenantId, userId);
        MessageNoticeVO[] noticeVOS = noticeMapper.getNoticesByUser(tenantId, userId, pageStart, pageSize);
        return new PageObject<>(noticeVOS, count, pageStart);
    }

    @Override
    public Boolean hasUnreadNotice(String user) throws BusinessException {
        String receiver = noticeMapper.hasUnreadNotice(user);
        return StringUtils.isNotEmpty(receiver);
    }

    @Override
    public int countUnreadNotice(String user) throws BusinessException {
        return noticeMapper.countUnreadByReceiver(user);
    }

}
